// import { combineReducers, applyMiddleware, createStore } from 'redux'
import { configureStore, EnhancedStore, Dispatch } from '@reduxjs/toolkit';
import loggerMiddleware from './middlewares/logger';

import userStore, { userOtherActions } from './modules/userStore';
import mainStore from './modules/mainStore';
import menuStore from './modules/menuStore';
import productStore from './modules/productStore';

let store: EnhancedStore;
// create store per-request
const makeStore = () => {
  const curStore = configureStore({
    reducer: {
      user: userStore.reducer,
      main: mainStore.reducer,
      menu: menuStore.reducer,
      product: productStore.reducer,
    },
    middleware(getDefaultMiddleware) {
      // return getDefaultMiddleware.concat(loggerMiddleware)

      const mws = getDefaultMiddleware();
      mws.push(loggerMiddleware);

      return mws;
    },
  });

  store = curStore;
  return curStore;
};

const actionCreators = {
  ...userStore.actions,
  ...userOtherActions,
  ...mainStore.actions,
  ...menuStore.actions,
  ...productStore.actions,
};

type Store = ReturnType<typeof makeStore>;
export type RootState = ReturnType<Store['getState']>;

export { store, makeStore, actionCreators };
